// user模块的异步逻辑
import { setToken,http, clearToken   } from '@/utils'

// 登录
export const login = (mobile,code)=>{
  return async (dispatch)=>{
    const res = await http.post('/authorizations',{
      mobile,code
    })
    // 注意：此处获取的是 token 
    const  token  = res.token
    setToken(token)
    dispatch({ type: 'user/setToken', payload: token })
  }
}
// 获取用户资料
export const getUserInfo = ()=>{
  return async (dispatch,getState)=>{
    const data = await http.get('/user/profile',{
      // headers: {
      //   Authorization: `Bearer ${getState().user.token}`
      // }
    })
    dispatch({ type: 'user/setName', payload: data.name })
  }
}
// 退出操作
export const logout = ()=>{
  return (dispatch,getState)=>{
    clearToken()
    dispatch({type:'user/setToken',payload:''})
    dispatch({type:'user/setName',payload:''})
  }
} 









